package cn.hs.lyxf.service.impl;

import cn.hs.lyxf.domain.Maintenance;
import cn.hs.lyxf.domain.MaintenanceWork;
import cn.hs.lyxf.exception.GlobalErrorCode;
import cn.hs.lyxf.mapper.MaintenanceWorkMapper;
import cn.hs.lyxf.result.JsonResult;
import cn.hs.lyxf.service.IMaintenanceService;
import cn.hs.lyxf.service.IMaintenanceWorkService;
import cn.hs.lyxf.utils.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzy
 * @since 2022-12-10
 */
@Service
public class MaintenanceWorkServiceImpl extends ServiceImpl<MaintenanceWorkMapper, MaintenanceWork> implements IMaintenanceWorkService {
    @Autowired
    private MaintenanceWorkMapper maintenanceWorkMapper;
    @Autowired
    private IMaintenanceService maintenanceService;

    /**
     * 维保员进行维保签到
     */
    @Override
    public JsonResult signIn(MaintenanceWork maintenanceWork) {
        AssertUtil.isNotNull(maintenanceWork, GlobalErrorCode.ERROR);
        MaintenanceWork work = maintenanceWorkMapper.selectById(maintenanceWork.getId());
        AssertUtil.isNotNull(work, GlobalErrorCode.ERROR);
        AssertUtil.isTrue(work.getState() == 0, GlobalErrorCode.ERROR);
        // 更新工作表
        work.setState(1);
        work.setSignInTime(new Date());
        work.setSignInPic(maintenanceWork.getSignInPic());
        maintenanceWorkMapper.updateById(work);
        return JsonResult.success();
    }

    /**
     * 维保人员上传问题记录
     */
    @Override
    public JsonResult problemRecord(MaintenanceWork maintenanceWork) {
        AssertUtil.isNotNull(maintenanceWork, GlobalErrorCode.ERROR);
        MaintenanceWork work = maintenanceWorkMapper.selectById(maintenanceWork.getId());
        AssertUtil.isNotNull(work, GlobalErrorCode.ERROR);
        AssertUtil.isTrue(work.getState() == 1, GlobalErrorCode.ERROR);
        // 更新工作表
        String riskPic = work.getRiskPic();
        if (riskPic == null) {
            work.setRiskPic(maintenanceWork.getRiskPic());
        } else {
            work.setRiskPic(riskPic + "," + maintenanceWork.getRiskPic());
        }
        work.setNote(maintenanceWork.getNote());
        maintenanceWorkMapper.updateById(work);
        return JsonResult.success();
    }

    /**
     * 维保完成
     */
    @Override
    public JsonResult finish(Long workId) {
        AssertUtil.isNotNull(workId, GlobalErrorCode.ERROR);
        MaintenanceWork work = maintenanceWorkMapper.selectById(workId);
        AssertUtil.isNotNull(work, GlobalErrorCode.ERROR);
        AssertUtil.isTrue(work.getState() == 1, GlobalErrorCode.ERROR);
        work.setFinishTime(new Date());
        work.setState(2);
        maintenanceWorkMapper.updateById(work);
        // 修改维保表月检状态
        EntityWrapper<Maintenance> wrapper = new EntityWrapper<>();
        wrapper.eq("user_company_id", work.getCompanyId());
        Maintenance maintenance = maintenanceService.selectOne(wrapper);
        Long maintenanceId = maintenance.getId();
        AssertUtil.isNotNull(maintenance, GlobalErrorCode.ERROR);
        maintenance.setType(1);
        maintenanceService.updateById(maintenance);
        return JsonResult.success();
    }
}
